package cn.jinjimi.framework.router;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import cn.jinjimi.app.bean.RouterConstants;
import cn.jinjimi.app.bean.RouterService;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.AppContext;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.framework.router.impl.IRTask;
import cn.jinjimi.framework.router.impl.RTaskFactory;
import cn.jinjimi.app.main.activity.LoginActivity;
import cn.jinjimi.app.main.activity.WebActivity;

import java.io.Serializable;

/**
 * Created by Roy
 * Date: 2017/3/9
 * @author zhenhua
 */
public class Router {

    private RouterOptions routerOptions;

    private boolean fourceLogin;

    public Router(Context context, String url) {
        routerOptions = new RouterOptions(url, context);
    }

    public static Router create(Context context, String url) {
        Router router = new Router(context, url);
        return router;
    }

    public Router addParam(String key, String value) {
        routerOptions.getBundle().putString(key, value);
        return this;
    }

    public Router addParam(String key, Serializable value) {
        routerOptions.getBundle().putSerializable(key, value);
        return this;
    }

    public Router addParam(String key, boolean value) {
        routerOptions.getBundle().putBoolean(key, value);
        return this;
    }

    public Router addParam(String key, int value) {
        routerOptions.getBundle().putInt(key, value);
        return this;
    }

    public Router addParam(String key, double value) {
        routerOptions.getBundle().putDouble(key, value);
        return this;
    }

    public Router addParam(Bundle bundle) {
        if (bundle != null) {
            routerOptions.getBundle().putAll(bundle);
        }
        return this;
    }

    public Router fourceLogin(boolean fourceLogin) {
        this.fourceLogin = fourceLogin;
        return this;
    }

    public void open() {
        if (StringUtils.isEmpty(routerOptions.getOriginUrl()))
            return;

        if (fourceLogin && !AccountManager.getInstance().isLogin()) {
            gotoLogin(routerOptions.getContext());
            return;
        }

        //权限检查
        if (!RouterHelper.isNeedLoginAndHasLogin(routerOptions.getOriginUrl())) {
            gotoLogin(routerOptions.getContext());
            return;
        }

        if (RouterHelper.isNativeUrl(routerOptions.getOriginUrl())) {
            openNativeView();//打开原生界面
        } else {
            openWebView();//打开网页
        }
    }

    private void openNativeView() {
        Object object = RouterService.ROUTER_TABLE.get(routerOptions.getTargetUrl());
        if (object != null) {
            IRTask task = RTaskFactory.buildRTask(object);
            if (task != null) {
                task.open(routerOptions);
            }
        }
    }

    private void openWebView() {
        Intent i = new Intent();
        if (routerOptions.getContext() == null) {
            routerOptions.setContext(AppContext.getInstance());
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        i.setClass(routerOptions.getContext(), WebActivity.class);
        routerOptions.getBundle().putInt(BundleKey.TYPE, 1);
        routerOptions.getBundle().putString(BundleKey.URL, routerOptions.getOriginUrl());

        i.putExtras(routerOptions.getBundle());
        routerOptions.getContext().startActivity(i);
    }

    /* 进入登录界面 */
    private void gotoLogin(Context context) {
        Intent intent = new Intent();
        if (context == null) {
            context = AppContext.getInstance();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.setClass(context, LoginActivity.class);
        intent.putExtra(BundleKey.PARAM_LOGIN_TARGETURL, routerOptions.getOriginUrl());
        intent.putExtra(BundleKey.PARAM_LOGIN_BUNDLE, routerOptions.getBundle());

        context.startActivity(intent);
        ToastUtils.toastMessage(context, "该功能需要登录后才能查看,请登录");
    }

    /* 提供给外部快速进入登陆界面 */
    public static void doLogin(Context context) {
        doLogin(context, null);
    }

    public static void doLogin(Context context, String loginSuccRedirectUrl) {
        Router router = Router.create(context, RouterConstants.getNativeRouter(RouterConstants.LOGIN));
        if (StringUtils.isNotEmpty(loginSuccRedirectUrl)) {
            router.addParam(BundleKey.PARAM_LOGIN_TARGETURL, loginSuccRedirectUrl);
        }
        router.open();
    }

    public RouterOptions getRouterOptions() {
        return routerOptions;
    }
}